'''
Orcish Horde made by FredDukes
'''
import es, wcs, gamethread, random
from wcs import wcs

RaceName = 'Orcish Horde'
race = wcs.Race(RaceName)

race.registerSkill('Critical Strike',6,2,'Have a chance of hitting a critical hit')
race.registerSkill('Critical Grenade',8,4,'You grenades will do additional damage')
race.registerSkill('Reincarnation',5,5,'Enables a random chance of respawning when you die')
race.registerUltimate('Chain Lightning',8,8,4,'Hold down the ultimate key to release a bolt of lightning')

def player_spawn(ev):
	uid = ev['userid']
	cl = wcs.GetLevel(uid, RaceName,'Chain Lightning')
	if cl:
		wcs.Command(uid).UpdateDict('light_power',0)
		
def player_hurt(ev):
	if int(ev['attacker']):
		uid, aid, uteam, ateam = ev['userid'], ev['attacker'], int(ev['es_userteam']), int(ev['es_attackerteam'])
		if uteam != ateam and aid: # If there on opposite sides, and the attacker is not 0, I.E the attacker is a player, and not 'World'
			CritStrike = wcs.GetLevel(aid, RaceName, 'Critical Strike')
			CritNade = wcs.GetLevel(aid, RaceName, 'Critical Grenade')
			if ev['weapon'] == "hegrenade" and CritNade:
				damage = random.randint(CritNade * 10,CritNade * 20)
				wcs.Command(uid).Damage(damage, 32, aid)
				es.tell(uid,'#multi','#greenYou have been hit by #lightgreen%s\'s #greenCritical Grenade'%ev['es_attackername'])
				es.tell(aid,'#multi','#greenYou just damage #lightgreen%s #greenwith your Critical Grenade'%ev['es_username'])
				wcs.Effect().Misc1(uid,4)
				return # Return the command, because if the weapon is a grenade, we don't want to carry on and test for critical damage
			if CritStrike:
				chance = random.randint(1,20) # Random intiger between 1 and 10
				if chance <= CritStrike:
					wcs.Command(uid).Damage(CritStrike * random.randint(3,6), 32, aid)
					es.tell(uid,'#multi','#greenYou have been hit by #lightgreen%s\'s #greenCritical Strike'%ev['es_attackername'])
					es.tell(aid,'#multi','#greenYou just damage #lightgreen%s #greenwith your Critical Strike'%ev['es_username'])
					wcs.Effect().Beam2(aid,uid,3,255,0,0)
				
def player_death(ev):
	uid = ev['userid']
	rl = wcs.GetLevel(uid, RaceName, 'Reincarnation')
	if rl:
		if random.randint(1,10) <= rl: gamethread.delayed(1,spawn_userid, uid)
		
def spawn_userid(uid):
	if int(es.getplayercount(es.getplayerteam(uid))):
		wcs.Command(uid).Spawn()
		wcs.Effect().Smoke(ev['userid'])
		es.tell(uid,'#multi','#lightgreenReincarnation #default- #greenYou have spawned due to your Reincanation spell.')
		
def player_ultimate_on(ev):
	uid = ev['userid']
	cl = wcs.GetLevel(uid, RaceName, 'Chain Lightning')
	if cl:
		a = wcs.Command(uid)
		a.SetCoolDown('Chain Lightning', 25)
		a.UpdateDict('light_power',0)
		delay_name = 'lightning_%s'%uid
		gamethread.delayedname(1.0, delay_name, AddPower , (uid, delay_name))
		
def AddPower(uid, DelayName):
	es.usermsg("create", "lightning_increase", "HintText")
	es.usermsg("write","short","lightning_increase",-1)
	a = wcs.Command(uid).ReturnDict('light_power')
	format = "Damage:\n-----------------------\n"
	if a < 10:
		es.emitsound('player',uid,'buttons/button19.wav',0.5,0.5)
		wcs.Command(uid).ModDict('light_power',1) 
		while a >= 0:
			a -= 1
			format += "|"
	else:
		format += "* MAX *"
	es.usermsg("write","string","lightning_increase",format)
	es.usermsg("send","lightning_increase",uid,0)
	es.usermsg("delete","lightning_increase")
	gamethread.delayedname(1.0, DelayName, AddPower, (uid, DelayName) )
	
def player_ultimate_off(ev):
	uid = ev['userid']
	cl = wcs.GetLevel(uid, RaceName, 'Chain Lightning')
	a = wcs.Command(uid)
	if cl:
		gamethread.cancelDelayed('lightning_%s'%uid)
		damage = a.ReturnDict('light_power')
		if damage <= 5: sound_list = ['spark4','spark5','spark6']
		else: sound_list = ['zap1','zap2','zap3']
		sound = 'ambient/energy/%s.wav'%random.choice(sound_list)
		es.emitsound('player',uid,sound,damage/10.0,damage/10.0)
		ident = '%s'%('#t' if ev['es_userteam'] == '3' else '#ct')
		a.Near(damage * 25,Damage,ident)
		
def Damage(uid, aid):
	u = wcs.Command(uid)
	a = wcs.Command(aid)
	damage = a.ReturnDict('light_power') * 2 * wcs.GetLevel(aid,RaceName,'Chain Lightning')
	wcs.Effect().Beam1(uid,aid,5,0,0,255)
	u.Damage(damage,32,aid)
